<?php
/*
 * @Description    : AiWsController
 * @Author         : QianLong
 * @Date           : 2023-10-25 17:59:48
 * @LastEditors    : QianLong
 * @Site           : http://www.21ds.cn
 * @LastEditTime   : 2023-10-26 12:24:51
 */

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Hyperf\WebSocketServer\Constant\Opcode;
use Swoole\Server;
use Swoole\WebSocket\Server as WebSocketServer;

class AiWsController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
  public function onMessage($server, $frame): void
  {
    if ($frame->opcode == Opcode::PING) {
      // 如果使用协程 Server，在判断是 PING 帧后，需要手动处理，返回 PONG 帧。
      // 异步风格 Server，可以直接通过 Swoole 配置处理，详情请见 https://wiki.swoole.com/#/websocket_server?id=open_websocket_ping_frame
      $server->push('', Opcode::PONG);
      return;
    }
    $server->push($frame->fd, 'Recv: ' . $frame->data);
  }
  public function testAuth($server, $frame): void
  {
    $server->push($frame->fd, 'this is test auth');
  }

  public function onClose($server, int $fd, int $reactorId): void
  {
    var_dump('closed');
  }

  public function onOpen($server, $request): void
  {
    $server->push($request->fd, 'Opened');
  }
}
